package com.villaday.idf.util;

import android.text.TextUtils;

import com.villaday.idf.adapter.Data;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by admin on 2018/9/6.
 */

public class DateUtils {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /*    public static void main(String args[]){
            //本周一
            System.out.println(getCurrentMonday());
            //本周二
            System.out.println(getPreviousSunday());
            //上周一
            System.out.println(getCurrentMonday(-7));
            //上周日
            System.out.println(getPreviousSunday(-7));
            //本月初
            System.out.println(getMinMonthDate("2017-04-17"));
            //本月末
            System.out.println(getMaxMonthDate("2017-04-17"));
            //上月初
            System.out.println(getMinMonthDate("2017-03-17",-1));
            //上月末
            System.out.println(getMaxMonthDate("2017-03-17",-1));
            //昨天
            System.out.println(getYesteroday());
        }*/
    // 获得本周一与当前日期相差的天数
    public static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    public static int getMondayPlus(int days) {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6 + days;
        } else {
            return 2 - dayOfWeek + days;
        }
    }

    // 获得当前周- 周一的日期
    public static String getCurrentMonday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
//        DateFormat df = DateFormat.getDateInstance();
        String preMonday = df.format(monday);
        return preMonday;
    }

    // 获得当前周- 周一的日期
    public static String getCurrentMonday(int days) {
        int mondayPlus = getMondayPlus(days);
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        DateFormat df = DateFormat.getDateInstance();
        String preMonday = df.format(monday);
        return preMonday;
    }


    // 获得当前周- 周日  的日期
    public static String getPreviousSunday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
        Date monday = currentDate.getTime();
//        DateFormat df = DateFormat.getDateInstance();
        java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
        String preMonday = df.format(monday);
        return preMonday;
    }

    // 获得当前周- 周日  的日期
    public static String getPreviousSunday(int days) {
        int mondayPlus = getMondayPlus(days);
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
        Date monday = currentDate.getTime();
        DateFormat df = DateFormat.getDateInstance();
        String preMonday = df.format(monday);
        return preMonday;
    }

    // 获得当前月--开始日期
    public static String getMinMonthDate(String date) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(dateFormat.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 获得当前月--开始日期
    public static String getMinMonthDate(String date, int month) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(dateFormat.parse(date));
            calendar.add(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 获得当前月--结束日期
    public static String getMaxMonthDate(String date) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(dateFormat.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getMaxMonthDate(String date, int month) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(dateFormat.parse(date));
            calendar.add(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getYesteroday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return dateFormat.format(calendar.getTime());
    }


    public static String getdate(int i) // //获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
    {
        Date dat = null;
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, i);
        dat = cd.getTime();
        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd");
        return dformat.format(dat);

    }


    /**
     * 获取当前日期
     *
     * @return
     */
    public static String GetNowDate() {
        String temp_str = "";
        Date dt = new Date();
        //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        temp_str = sdf.format(dt);
        return temp_str;
    }

    /**
     * 获得两个日期之间的天数
     */
    public static long getDatesDays(String EndDate, String startDate) throws ParseException {
        long day = 0;
        Date a1 = new SimpleDateFormat("yyyy-MM-dd").parse(EndDate);
        Date b1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
        day = (a1.getTime() - b1.getTime()) / (24 * 60 * 60 * 1000);
        return day;

    }

    public static int getYear() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.YEAR);
    }


    public static boolean isMoreThan6points(int datehours) {//
        Calendar datec = Calendar.getInstance();
        int hours = datec.getTime().getHours();

        return hours < datehours;
    }

    public static String getCalculateDay(Object initTime, int n) throws ParseException {
        //返回推算后的日期
        String calculateDay = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d");
        //初始日期
        Date initDate = null;
        if (null == initTime) {
            initDate = new Date();
        } else if (initTime instanceof Date) {
            initDate = (Date) initTime;
        } else {
            initDate = format.parse((String) initTime);
        }

        //转换成距离1970年0时0分的毫秒数
        long initMillisenconds = initDate.getTime();
        long oneDayTime = 24 * 60 * 60 * 1000;
        initMillisenconds += oneDayTime * n;

        //推算出日期
        Date calculateDate = new Date(initMillisenconds);
        calculateDay = format.format(calculateDate);
        return calculateDay;
    }


    public static int getMonth() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.MONTH) + 1;
    }

    public static String getStringDate(String date) {
        String[] split = date.split("-");
        return split[1] + "." + split[2];
    }


    public static String getStringMouth(long date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(date);
        return dateString;
    }


    public static String String2Data(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }


    /**
     * 根据提供的年月日获取该月份的第一天
     *
     * @param year
     * @param monthOfYear
     * @return
     * @Description: (这里用一句话描述这个方法的作用)
     * @Author: gyz
     * @Since: 2017-1-9下午2:26:57
     */
    public static Date getSupportBeginDayofMonth(int year, int monthOfYear) {
        Calendar cal = Calendar.getInstance();
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, monthOfYear);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        cal.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDate = cal.getTime();

        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDate = cal.getTime();
        return firstDate;
    }

    /**
     * 根据提供的年月获取该月份的最后一天
     *
     * @param year
     * @param monthOfYear
     * @return
     * @Description: (这里用一句话描述这个方法的作用)
     * @Author: gyz
     * @Since: 2017-1-9下午2:29:38
     */
    public static Date getSupportEndDayofMonth(int year, int monthOfYear) {
        Calendar cal = Calendar.getInstance();
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, monthOfYear);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);

        cal.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDate = cal.getTime();

        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDate = cal.getTime();
        return lastDate;
    }

    /**
     * 将可视时间转行为long整形数字
     *
     * @param dataString 可读时间字符串
     * @return 长整型
     */
    public static long string2Data(String dataString) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sf.parse(dataString);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 长整型转化为可视的时间类型的字符串
     *
     * @return 可读性时间
     */
    public static String long2Data(long numData) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(numData);
        return sf.format(date);
    }


    /**
     * 长整型转化为可视的时间类型的字符串
     *
     * @return 可读性时间
     */
    public static String String2String(String StringData) {
        if (!TextUtils.isEmpty(StringData)) {
            return StringData.substring(2);
        } else {
            return "--";
        }
    }

    /**
     * @return 可读性时间
     */
    public static String data2Data(String numData) {
        if (TextUtils.isEmpty(numData)) {
            return "";
        } else {
            String[] split = numData.split("-");
            return split[1] + "月" + split[2] + "日";
        }

    }

}

